<?php 
namespace Admin\Controller;
use Think\Controller;

class OrderController extends AdminController
{
	public function index()
	{
		$ord = M('order');
		$list = $ord->select();
		$this->assign('list',$list);
		$this->display('Order/index');
	}

	public function edit()
	{
		// var_dump($_GET);
		$ord = M('order');
		$id = $_GET['id'];
		$type = $_GET['type'];
		$num = $_GET['num'];
		$val = '';
		switch ($type) {
			case 'sendstatus':
				if($num == 0){
					$num = 1;
				}elseif ($num == 1) {
					$num = 2;
				}elseif ($num == 2) {
					$num = 0;
				}
				break;
			case 'paystatus':
				if($num == 0){
					$num = 1;
				}elseif ($num == 1) {
					$num = 0;
				}
				break;
			case 'cancle':
				if($num == 0){
					$num = 1;
				}elseif ($num == 1) {
					$num = 0;
				}
				break;
		}
		$list[$type] = $num;
		// var_dump($id);
		// var_dump($list);
		if($ord->create($list)){
			$ord->where('id='.$id)->save();
		}

	}
	//删除订单
	public function del()
	{
		// var_dump($_GET['id']);
		$id = $_GET['id'];
		$ord = M('order');
		$orda = M('orderdata');

		$ordernum = $ord->field('ordernum')->where('id='.$id)->select();
		$ordernum = $ordernum[0]['ordernum'];
		$list['ordernum']=$ordernum;
		// var_dump($ordernum);die;
		// var_dump($orda->create($ordernum));

		// $orda->fetchSql(true)->delete();
		//先删除该订单下面的所有商品，若删除成功了就删除该订单；
		$re = $orda->where(array('ordernum'=>$ordernum))->select();
		// var_dump($re);die;
		if(empty($re)){
			if($ord->where(array('id='.$id))->delete()){
				$this->success('删除成功');
			}else{
				$this->success('删除失败');
			}
		}else{
			$res = $orda->where(array('ordernum'=>$ordernum))->delete();
			if($res){		
				if($ord->where(array('id='.$id))->delete()){
					$this->success('删除成功');
				}else{
					$this->success('删除失败');
				}
				
			}
		}
		
	}
	
}